package com.jketing.test;

import java.lang.reflect.InvocationTargetException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
import java.util.Set;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.jpa.domain.Specification;

public class Temp {

	public static Specification getSpecification(Object object) {
		Specification specification = null;
		if (object != null && !"".equals(object)) {
			SimpleDateFormat df = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
			try {
				Map<String, String> map = BeanUtils.describe(object);
				Set<String> keySet = map.keySet();
				for (Iterator<String> iter = keySet.iterator(); iter.hasNext();) {
					String element = iter.next();
					String temp = map.get(element);
					if (StringUtils.isNotBlank(temp)) {
						if (!"class".equals(element)) {
							String keyClass = PropertyUtils.getPropertyType(object,element).getName();
								if (!keyClass.contains("com.")) { // 处理非Bean类型
									if ("java.lang.String".equals(keyClass)) {
										Date d = df.parse(temp);
									} else if ("java.util.Date".equals(keyClass)) {
										Date d = df.parse(temp);
									} else if ("java.lang.Byte".equals(keyClass)) {
										byte val = Byte.parseByte(temp);
										if (val != 0) {// 数值型属性不等0才加条件
											//sb.append(" and " + prfx + element + " <=" + map.get(element) + " ");
										}
									}else if ("java.lang.Short".equals(keyClass)) {
										short val = Short.parseShort(temp);
										if (val != 0) {
											//sb.append(" and " + prfx + element + " <=" + map.get(element) + " ");
										}
									}  else if ("java.lang.Integer".equals(keyClass)) {
										int val = Integer.parseInt(temp);
										if (val != 0) {// 不等0才加条件
											//sb.append(" and " + prfx + element + " <=" + map.get(element) + " ");
										}
									}else if ("java.lang.Long".equals(keyClass)) {
										long val = Long.parseLong(temp);
										if (val != 0) {// 不等0才加条件
											//sb.append(" and " + prfx + element + " <=" + map.get(element) + " ");
										}
									} else if ("java.lang.Float".equals(keyClass)) {
										float val = Float.parseFloat(temp);
										if (val != 0) {// 不等0才加条件
											//sb.append(" and " + prfx + element + " <=" + map.get(element) + " ");
										}
									} else if ("java.lang.Double".equals(keyClass)) {
										double val = Double.parseDouble(temp);
										if (val != 0) {// 不等0才加条件
											//sb.append(" and " + prfx + element + " <=" + map.get(element) + " ");
										}
									} // 处理非模糊查询
									else if (map.get(element).toString().startsWith("^") || "id".equals(element)) {
										String v = map.get(element).toString();
										if (map.get(element).toString().startsWith("^"))
											v = v.substring(1, v.length());
										//sb.append(" and " + prfx + element + " = '" + v + "' ");
									} else if ("java.util.Set".equals(keyClass)) {
										//sb.append(" and " + prfx + element + " like '%" + map.get(element) + "%' ");
									}
								} 
								else {// 处理类型为Bean的属性
									 Object tempObj = PropertyUtils.getProperty(object, element.toString());//递归处理引用的Bean属性
									 System.out.println(tempObj);
									 //sb.append(objectToHqlStr(prfx + element.toString(), tempObj, params));
								}
						}
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return specification;
	}
	
	public static void main(String[] args) {
		TestBean tt = new TestBean();
		tt.setId("111");
		tt.setA(1);
		tt.setB((byte)2);
		tt.setC('A');
		tt.setD(4.4);
		tt.setF((float)5.5);
		tt.setL(6);
		tt.setS((short)7);
		tt.setBool(true);
		tt.setStr("jketing");
		SubBean sub = new SubBean();
		sub.setId("333");
		sub.setName("test");
		sub.setTime(new Date());
		tt.setSub(sub);
		HashSet<SubBean> set = new HashSet<SubBean>();
		set.add(sub);
		tt.setSet(set);
		Specification specification = getSpecification(tt);
	}

}
